package com.face.mapper;


import com.face.entity.User;
import com.face.wrapper.UserWrapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {


    //keyProperty  值是Java类属性
    //后选择主键，先执行插入     before=false
    @Insert("insert into tb_user(id,userId, shopId, username, gender, age,job,imgPath,type,`group`, creator,createTime,updateTime,isDelete,visitCount,mobilephone) values(#{id},#{userId},#{shopId},#{username},#{gender},#{age},#{job},#{imgPath},#{type},#{group},#{creator},#{createTime},#{updateTime},#{isDelete},#{visitCount},#{mobilephone})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = Integer.class)
    Integer saveUser(User user);

    @Select("select * from tb_user where userId = #{userId} limit 1")
    User getUserById(@Param("userId") String userId);

    @Select("<script>select * from tb_user where shopId = #{shopId} " +
            "<if test='username!=null'>and userName like CONCAT('%',#{username},'%')</if> " +
            "<if test='type!=null'>and type=#{type}</if> " +
            "<if test='creator!=null'>and creator=#{creator}</if> " +
            "order by ${order}</script>")
    List<User> listAll(@Param("shopId") String shopId, @Param("order") String order, String username, String type, String creator);

    @Select("<script>select * from tb_user where shopId = #{shopId} " +
            "<if test='start!=null and end!=null'>and createTime between #{start} and #{end} </if>" +
            "<if test='username!=null'>and userName like CONCAT('%',#{username},'%')</if> " +
            "<if test='type!=null'>and type=#{type}</if> " +
            "<if test='creator!=null'>and type=#{creator}</if> " +
            "order by ${order}</script>")
    List<User> list(@Param("shopId") String shopId, @Param("start") String start, @Param("end") String end, @Param("order") String order, String username,
                    String type, String creator);

    @Select("select count(*) from tb_user where shopId = #{shopId} and updateTime between #{start} and #{end}")
    Integer count(@Param("shopId") String shopId, @Param("start") String start, @Param("end") String end);

    @Select("select count(*) from tb_user where shopId = #{shopId}")
    Integer countAll(@Param("shopId") String shopId);

    @Update("<script>" +
            "update tb_user set <if test='username!=null'>username=#{username},</if>" +
            "<if test='type!=null'>type=#{type},</if> " +
            "<if test='group!=null'>`group`=#{group},</if>" +
            "<if test='mobilephone!=null'>mobilephone=#{mobilephone},</if>" +
            "<if test='visitCount!=null'>visitCount=#{visitCount},</if>" +
            "<if test='updateTime!=null'>updateTime=#{updateTime}</if>" +
            " where id=#{id}" +
            "</script>")
    Integer updateUser(User user);

    @Select("select DATE_FORMAT(createTime,'%Y-%m-%d') date, count(*) count from tb_user where shopId = #{shopId} and DATE_FORMAT(createTime,'%Y-%m-%d') between DATE_FORMAT(#{start},'%Y-%m-%d') and DATE_FORMAT(#{end},'%Y-%m-%d') group by date")
    List<Map<String, Object>> dailyCount(@Param("shopId") String shopId, @Param("start") String start, @Param("end") String end);

    @Select("select DATE_FORMAT(createTime,'%Y-%m') date, count(*) count from tb_user where shopId = #{shopId} and DATE_FORMAT(createTime,'%Y-%m') between #{start} and #{end} group by date")
    List<Map<String, Object>> monthlyCount(@Param("shopId") String shopId, @Param("start") String start, @Param("end") String end);

    @Select("select a.*, b.openid from tb_user a left join tb_account b on a.creator = b.username where a.userId = #{userId}")
    UserWrapper findByUserId(@Param("userId") String userId);

    @Select("select a.userName, a.userId, a.imgPath, max(b.loginTime) updateTime from tb_user a left join tb_visit b on a.userId=b.userId where a.creator = #{creator} group by a.userId order by ${order} desc")
    List<User> findUserByCreator(@Param("creator") String creator, @Param("order") String order);

    @Select("select a.userName, a.userId, a.imgPath, max(b.loginTime) updateTime from tb_user a left join tb_visit b on a.userId=b.userId where b.shopId = #{shopId} group by a.userId order by ${order} desc")
    List<User> findUserForManager(@Param("shopId") String shopId, @Param("order") String order);

    @Select("select username, creator, type from tb_user where shopId=#{shopId}")
    List<User> findAll(String shopId);
}
